import axios from "axios";
import { useUserStore, useLoadStore } from "@/stores";
import { ElMessage } from "element-plus";
import router from "@/router";
const useUser = useUserStore();
const loadStore = useLoadStore();
export const request = axios.create({
  timeout: 5000,
});
request.interceptors.request.use(
  (config) => {
    loadStore.load = true;
    config.headers = {
      "Content-Type": "application/json;charset=UTF-8",
      Authorization: "Bearer " + useUser.token,
    };
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);
request.interceptors.response.use(
  (response) => {
    if (response.data.status === 401) {
      ElMessage.warning("登录过期，请重新登录");
      router.push("/login");
    } else if (response.data.status === 400) {
      ElMessage.warning(response.data.message);
    }
    loadStore.load = false;
    return response.data;
  },
  (error) => {
    loadStore.load = false;

    console.log(error, "错误");
    return Promise.reject(error);
  }
);
